<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button class="qqq" type="primary" @click="dialogVisible = true">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolelist" stripe border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom', i1===0 ? 'bdtop' : '', 'vcanni']" v-for="(item1, i1) in scope.row.children"
                    :key="item1.id">
              <el-col :span="5">
                <el-tag closable @close="romvebaid(scope.row, item1.id)">{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row :class="[i2 === 0 ? '' : 'bdtop', 'vcanni']" v-for="(item2 ,i2) in item1.children"
                        :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="romvebaid(scope.row, item2.id)">{{
                        item2.authName
                      }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag type="warning" v-for="(item3) in item2.children" :key="item3.id" closable
                            @close="romvebaid(scope.row, item3.id)">{{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" prop="level" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="EditAddfrom(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="open(scope.row.id)">删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="quanxianliebiao(scope.row)">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="添加角色"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closedialog">
      <el-form :model="AddForm" :rules="AddFormRules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="AddForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="AddForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="Addjuese">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="编辑角色"
      :visible.sync="editdialogVisible"
      width="30%"
      @close="editclosedialog">
      <el-form :model="EditForm" :rules="EditFormRules" ref="EditruleForm" label-width="100px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="EditForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="EditForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="Rditjuese">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="权限列表"
      :visible.sync="quanxiandialogVisible"
      width="50%"
      @close="guanbi">
      <el-tree :data="huoququanxianliebiao" :props="defaultquanxianProps" show-checkbox default-expand-all
               node-key="id" :default-checked-keys="Keysquanxian" ref="getkeys"></el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="quanxiandialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="getxuanzhong">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      rolelist: [],
      Keysquanxian: [],
      Strkey: [],
      huoququanxianliebiao: [],
      dialogVisible: false,
      editdialogVisible: false,
      quanxiandialogVisible: false,
      AddForm: {},
      EditForm: {},
      roleID: '',
      defaultquanxianProps: {
        children: 'children',
        label: 'authName'
      },
      AddFormRules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 6,
            message: '长度在 3 到 6 个字符',
            trigger: 'blur'
          }
        ],
        roleDesc: [
          {
            message: '请输入角色描述',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      EditFormRules: {
        roleName: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 6,
            message: '长度在 3 到 6 个字符',
            trigger: 'blur'
          }
        ],
        roleDesc: [
          {
            message: '请输入角色描述',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '长度在 3 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色失败')
      }
      this.rolelist = res.data
    },
    Addjuese () {
      this.$refs.ruleForm.validate(async value => {
        if (!value) return
        const { data: res } = await this.$http.post('roles', this.AddForm)
        if (res.meta.status !== 201) {
          this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.dialogVisible = false
        this.getRoleList()
      })
    },
    async EditAddfrom (id) {
      const { data: res } = await this.$http.get('roles/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色失败')
      }
      this.EditForm = res.data
      this.editdialogVisible = true
    },
    Rditjuese () {
      this.$refs.EditruleForm.validate(async value => {
        if (!value) return
        const { data: res } = await this.$http.put('roles/' + this.EditForm.roleId, {
          roleName: this.EditForm.roleName,
          roleDesc: this.EditForm.roleDesc
        })
        if (res.meta.status !== 200) {
          return this.$message.error('修改信息失败')
        }
        this.editdialogVisible = false
        this.$message.success('修改信息成功')
        this.getRoleList()
      })
    },
    async open (id) {
      const confirmButton = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).catch(err => err)
      if (confirmButton !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('roles/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除成功')
      this.getRoleList()
    },
    closedialog () {
      this.$refs.ruleForm.resetFields()
    },
    editclosedialog () {
      this.$refs.EditruleForm.resetFields()
    },
    async romvebaid (role, rightId) {
      const confirmrelt = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmrelt !== 'confirm') {
        return this.$message.info('取消删除操作')
      }
      const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
      if (res.meta.status !== 200) {
        this.$message.error('删除权限失败')
      }
      role.children = res.data
    },
    async quanxianliebiao (role) {
      this.roleID = role.id
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限失败')
      }
      this.huoququanxianliebiao = res.data
      this.getLeadfKey(role, this.Keysquanxian)
      this.quanxiandialogVisible = true
    },
    getLeadfKey (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getLeadfKey(item, arr))
    },
    guanbi () {
      this.Keysquanxian = []
    },
    async getxuanzhong () {
      const key = [
        ...this.$refs.getkeys.getCheckedKeys(),
        ...this.$refs.getkeys.getHalfCheckedKeys()
      ]
      const Strkey = key.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleID}/rights`, { rids: Strkey })
      if (res.meta.status !== 200) {
        return this.$message.error('操作失败')
      }
      this.getRoleList()
      this.quanxiandialogVisible = false
    }
  }
}
</script>

<style scoped>
.el-breadcrumb {
  font-size: 12px;
  margin-bottom: 15px;
}

.el-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}

.qqq {
  margin-bottom: 20px;
}

.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 1px solid #eee;
}

.bdbottom {
  border-bottom: 1px solid #eee;
}

.vcanni {
  display: flex;
  align-items: center;
}
</style>
